package com.fencing_squad.homo.page;

import com.bumptech.glide.Glide;
import com.fencing_squad.homo.ResourceTable;
import com.fencing_squad.homo.util.Data;
import com.fencing_squad.homo.util.function.FunctionComponent;
import com.fencing_squad.homo.util.ImageLoader;
import com.fencing_squad.homo.util.function.FunctionVoid;
import de.hdodenhof.circleimageview.CircleImageView;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.app.Context;

public class User {
    private Component user = null;
    private Component userInfo;
    private Component userBalance;
    private Component userPost;
    private Component userOffer;
    private CircleImageView userIcon;
    private Text nickname;
    private Text uid;
    private Text userVerified;
    private Text userMoney;
    private Text userPoint;
    private Text userPostDetail;
    private Text userOfferDetail;

    public Component get(Context context, FunctionComponent handleUserClicked, FunctionComponent handlePostClicked, FunctionComponent handleBalanceClicked, FunctionComponent handleOfferClicked, FunctionVoid redirectToLogin) {
        if(user == null) {
            user = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_user, null, false);
        }
        userIcon = (CircleImageView) user.findComponentById(ResourceTable.Id_userIcon);
        nickname = (Text) user.findComponentById(ResourceTable.Id_userNickname);
        uid = (Text) user.findComponentById(ResourceTable.Id_userUID);
        userVerified = (Text) user.findComponentById(ResourceTable.Id_userVerified);
        userMoney = (Text) user.findComponentById(ResourceTable.Id_userMoney);
        userPoint = (Text) user.findComponentById(ResourceTable.Id_userPoint);
        userPostDetail = (Text) user.findComponentById(ResourceTable.Id_userPostDetail);
        userOfferDetail = (Text) user.findComponentById(ResourceTable.Id_userOfferDetail);
        userInfo = user.findComponentById(ResourceTable.Id_userInfo);
        userBalance = user.findComponentById(ResourceTable.Id_userBalance);
        userPost = user.findComponentById(ResourceTable.Id_userPost);
        userOffer = user.findComponentById(ResourceTable.Id_userOffer);

        userIcon.setClickedListener(component -> {
            if (!Data.Account.isLogin) {
                redirectToLogin.func();
            }
        });

        userInfo.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                handleUserClicked.func(component);
            }
        });

        userPost.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                handlePostClicked.func(component);
            }
        });

        userBalance.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                handleBalanceClicked.func(component);
            }
        });

        userOffer.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                handleOfferClicked.func(component);
            }
        });
        update();
        return user;
    }

    public void update() {
        nickname.setText(Data.Account.nickname.equals("") ? "请登录" : Data.Account.nickname);
        uid.setText(Data.Account.uid.equals("") ? "UID:--" : "UID:"+ Data.Account.uid);
        userMoney.setText("￥" + String.valueOf(Data.Account.money));
        userPoint.setText(String.valueOf(Data.Account.point) + "分");
        userPostDetail.setText(String.valueOf(Data.Account.postMessageNum) + "篇");
        userOfferDetail.setText(String.valueOf(Data.Account.rewardNum) + "个");
        userVerified.setText(Data.Account.status.equals("") ? "游客" : Data.Account.status);
        Glide.with(user.getContext()).load(Data.Account.avatar).listener(ImageLoader.fail(userIcon, ResourceTable.Media_icon)).into(userIcon);
    }
}
